<?php

/**
 * usuarios actions.
 *
 * @package    ibe
 * @subpackage usuarios
 * @author     Creatix
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class usuariosActions extends sfActionsCreatix
{
  public function executeIndex(sfWebRequest $request)
  {
    return $this->forward('usuarios', 'list');
  }

	public function executeGrid()
	{
		//Recupero todas as informações necessárias para trabalhar com o grid
		$this->getRequestDataTable();
		$this->data					= sfGuardUserProfileTable::consultar($this->filter, $this->order);
		$this->total_data			= count($this->data);
		$this->data					= $this->paginadorDataTable($this->data, $this->page_num, parent::LIMIT_PAGE_DATA_TABLE);

		sfLoader::loadHelpers(array('Url'));

	    foreach($this->data as $key => $r)
	    {
	    					    	$actions = array(
				'edit'	 => "<a href='".url_for($this->getModuleName().'/edit?id_profile='.$r['id_profile'])."' class='icoEditar'></a>",
				'delete' => "<a href='".url_for($this->getModuleName().'/delete?id_profile='.$r['id_profile'])."'  class='icoExcluir delete'></a>",
			);
																																																																									    	$this->data[$key]['act'] = implode(' ', $actions);
    	}

		$this->setJSONDataTable($this->data, $this->colunas, $this->total_data);
		return sfView::NONE;
	}


    public function executeList()
    {
        $this->form = new sfGuardUserProfileForm();
    	$this->colunas	= array(
    		      		$this->form['id_profile']->renderLabel() => "id_profile",
			      		$this->form['id_usuario']->renderLabel() => "id_usuario",
			      		$this->form['id_usuario_pai']->renderLabel() => "id_usuario_pai",
			      		$this->form['no_usuario']->renderLabel() => "no_usuario",
			      		$this->form['nu_cpf']->renderLabel() => "nu_cpf",
			      		$this->form['sobrenome']->renderLabel() => "sobrenome",
			      		$this->form['email']->renderLabel() => "email",
			      		$this->form['nu_ddd_tel']->renderLabel() => "nu_ddd_tel",
			      		$this->form['nu_tel']->renderLabel() => "nu_tel",
			      		$this->form['st_estado_civil']->renderLabel() => "st_estado_civil",
			      		$this->form['sexo']->renderLabel() => "sexo",
			      		$this->form['dt_nascimento']->renderLabel() => "dt_nascimento",
						"Ações"	=> "act"
		);

        $this->filter = $this->_getFilter();

        $this->setTemplate('index');
    }

    public function executeFilter()
	{
	    $filter = new Filtro(
			$this->getRequestParameter(sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE)
		);

		$filter->setCulture($this->getUser()->getCulture());
		$this->getUser()->setAttribute('pg_num',1,$this->getModuleName());

		$this->getUser()->setAttribute(
			sfActionsCreatix::FILTER_SESSION_USER_ATTRIBUTE,
			serialize($filter),
			$this->getModuleName()
		);

		return $this->forward($this->getModuleName(), '/list');
	}

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new sfGuardUserProfileForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new sfGuardUserProfileForm();

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'new');
  }

  public function validateCreate()
  {
  	return true;
  }

  public function handleErrorCreate()
  {
    return $this->forward($this->getModuleName(), 'new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($dados_usuario = Doctrine::getTable('sfGuardUserProfile')->find(array($request->getParameter('id_profile'))), sprintf('Object dados_usuario does not exist (%s).', $request->getParameter('id_profile')));
    $this->form = new sfGuardUserProfileForm($dados_usuario);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($dados_usuario = Doctrine::getTable('sfGuardUserProfile')->find(array($request->getParameter('id_profile'))), sprintf('Object dados_usuario does not exist (%s).', $request->getParameter('id_profile')));
    $this->form = new sfGuardUserProfileForm($dados_usuario);

    $this->processForm($request, $this->form);

    $this->forward($this->getModuleName(), 'edit');
  }

  public function validateUpdate()
  {
  	return true;
  }

  public function handleErrorUpdate()
  {
    return $this->forward($this->getModuleName(), 'edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    try
    {
    $request->checkCSRFProtection();

    $this->forward404Unless($dados_usuario = Doctrine::getTable('sfGuardUserProfile')->find(array($request->getParameter('id_profile'))), sprintf('Object dados_usuario does not exist (%s).', $request->getParameter('id_profile')));
    $dados_usuario->delete();

    $this->redirect('usuarios/index');
    }
    catch(Excepiton $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
    	$this->redirect('usuarios/index');
	}

  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
  	try
  	{
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $dados_usuario = $form->save();
	  $this->getUser()->setFlash('mensagem', 'Dados salvos com sucesso.');
      $this->redirect('usuarios/edit?id_profile='.$dados_usuario->getIdsfGuardUserProfile());
    }
    else
    {
    throw new Exception($form->getErrorSchema());
    }
    }
    catch(Exception $e)
	{
		$this->getUser()->setFlash('erro', $e->getMessage());
	}
  }
}
